	<?php 
	require_once("MoneyFactory.class.php"); 
	require_once("PorteMonnaie.class.php");


	//INITIALISATION DES VARIABLES ET DU PORTE MONNAIE
	//m2,m3,m4 sont respectivement les poches euros, dollar et franc suisse du porte monnaie
	//wallet est le porte monnaie complet
	if(isset($_POST['m2']) ) {
		$m2 = MoneyFactory::getDefaultFactory()->createMoney($_POST['m2'],"EUR");
		$m3 = MoneyFactory::getDefaultFactory()->createMoney($_POST['m3'],"USD");
		$m4 = MoneyFactory::getDefaultFactory()->createMoney($_POST['m4'],"CHF");
		$wallet =[$m2,$m3,$m4];
	}
	else{
		$m2 = MoneyFactory::getDefaultFactory()->createMoney(0,"EUR");
		$m3 = MoneyFactory::getDefaultFactory()->createMoney(0,"USD");
		$m4 = MoneyFactory::getDefaultFactory()->createMoney(0,"CHF");

		echo "<div id='result'>Votre porte monnaie contient : <br/>";

		echo "(EUR) 0 <br/>(USD) 0 <br/>(CHF) 0 <br/>  </div>";

		$wallet =[$m2,$m3,$m4];
	}

	//fonction principale représentant les transactions habituelles
	function processRequest($request) {
		global $m2;
		global $m3;
		global $m4;
		global $wallet;

		if (isset($request["ops"]) && isset($request["v1"]) && isset($request["c1"]))
		{
			$m1=MoneyFactory::getDefaultFactory()->createMoney($request["v1"],$request["c1"]);


			//on recupere l'operation voulue (ajout ou retrait de pieces au porte monnaie)
			$opsCode=$request["ops"];

			echo "<div id='result'>Votre porte monnaie contient : </div>";
			echo "<div id='result_detail'>";


			//on effectue la transaction demandée
			try{
				if (strcmp($opsCode,"ADD")==0) {
					$res=PorteMonnaie::addMulti($m1,$wallet);

				}
				if (strcmp($opsCode,"REM")==0) {
					$res=PorteMonnaie::supprMulti($m1,$wallet);
				}

			} catch (NotEnoughMoneyException $e){
				//en cas de monnaie insuffisante on indique à l'utilisateur que le porte monnaie n'est pas assez fourni
				$res=$wallet;
				echo "<script type=\"text/javascript\">
				alert(\"Vous n\'avez pas assez d\'argent pour effectuer cette opération\");
				</script>";

			}
			
			if (isset($res)) {
				//on met à jour le contenu du porte monnaie
				echo "(",$res[0]->getCurrency(),") ",$res[0]->getValue();
				echo "<br/>(",$res[1]->getCurrency(),") ",$res[1]->getValue();
				echo "<br/>(",$res[2]->getCurrency(),") ",$res[2]->getValue();

			} 
			else echo "Unsupported operations"; 
			
			$m2 = MoneyFactory::getDefaultFactory()->createMoney($res[0]->getValue(),$res[0]->getCurrency());
			$m3 = MoneyFactory::getDefaultFactory()->createMoney($res[1]->getValue(),$res[1]->getCurrency());
			$m4 = MoneyFactory::getDefaultFactory()->createMoney($res[2]->getValue(),$res[2]->getCurrency());



			echo "</div>";
			
		}
		
	}

	//fonction représentant la demande de conversion d'une partie du porte monnaie en une autre devise
	function convertRequest($req){
		global $wallet;
		global $m2;
		global $m3;
		global $m4;

		if (!empty($req["val1"]) && !empty($req["dev1"]) && !empty($req["dev2"])) {

			$v1 = $req["val1"];		
			$d1 = $req["dev1"];
			$d2 = $req["dev2"];

		//monnaie avant la conversion
			$m = new Money($v1,$d1);
		//monnaie aprés la conversion
			$mdest = Money::convertTo($m,$d2);

		//on met à jour le porte monnaie
			$res = PorteMonnaie::supprMulti($m,$wallet);
			$res2 = PorteMonnaie::addMulti($mdest,$res);
			$m2 = MoneyFactory::getDefaultFactory()->createMoney($res2[0]->getValue(),$res2[0]->getCurrency());
			$m3 = MoneyFactory::getDefaultFactory()->createMoney($res2[1]->getValue(),$res2[1]->getCurrency());
			$m4 = MoneyFactory::getDefaultFactory()->createMoney($res2[2]->getValue(),$res2[2]->getCurrency());
			echo "<script type=\"text/javascript\">
			document.getElementById(\"m2\").value=".$m2->getValue().";"."document.getElementById(\"m3\").value=".$m3->getValue().";"."document.getElementById(\"m4\").value=".$m4->getValue()."; 
			document.getElementById(\"conv\").click();</script>";

		}

	}
	?>
	<html>
	<head>
		<title>Money Operations</title>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
	</head>
	<body>
		<?php
		processRequest($_REQUEST);
		?>
		<hr width='20%' align='left'/>
		<form action='index.php' method='post' id='form0'>
			<!-- variables servant à la persistance des données --> 
			<input type='hidden' id='m2' name='m2' value='<?php echo $m2->getValue();?>'/>
			<input type='hidden' id='m3' name='m3' value='<?php echo $m3->getValue(); ?>'/>
			<input type='hidden' id='m4' name='m4' value='<?php echo $m4->getValue(); ?>'/>
			
			<table>
				<tr>
					<td>Valeur</td><td>Devise</td><td>Opération</td>
				</tr>
				<tr>
					<td><input type='text' id='v1' name='v1' value=''/></td>
					<td><input type='text' id='c1' name='c1' value=''/></td>
					<td><select name='ops'><option value='ADD'>+</option><option value='REM'>-</option></select></td>
				</tr>
				<tr>
					<td colspan='5' align='right'><input type='submit' value='calculer'/></td>			
				</tr>
			</table>


			<hr width='20%' align='left'/>
			<h3>Conversion</h3>
			
			
			<table>

				<tr>
					<td>Valeur</td><td>Devise src</td><td>Devise dest</td>
				</tr>
				<tr>
					<td><input type='text' id='val1' name='val1' value=""/></td>
					<td><input type='text' id='dev1' name='dev1' value=""/></td>
					<td><input type='text' id='dev2' name='dev2' value=""/></td>

				</tr>
				<tr>
					<td colspan='5' align='right'><input id="conv" type='submit' value='convertir'/></td>			
				</tr>
			</table>
			<?php
			convertRequest($_REQUEST);
			?>
		</form>
		

		
	</body>
	</html>
